package com.book.workbench.service.impl;

import com.book.common.UUidUtil;
import com.book.settings.pojo.User;
import com.book.workbench.mapper.BookMapper;
import com.book.workbench.mapper.KindMapper;
import com.book.workbench.pojo.Kind;
import com.book.workbench.service.KindService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class KindServiceImpl implements KindService {

    @Autowired
    private KindMapper kindMapper;

    @Autowired
    private BookMapper bookMapper;


    @Override
    public Integer addKind(Kind kind) {
        Kind kind1 = kindMapper.getKindByType(kind);
        if(kind1==null){
            kind.setId(UUidUtil.getUUID());
            return kindMapper.addKind(kind);
        }else{
            return -1;
        }
    }

    @Override
    public PageInfo<Kind> getListKind(Kind kind) {
        PageHelper.startPage(kind.getPageNo(),kind.getPageSize());
        List<Kind> kinds = kindMapper.getListKinds(kind);
        PageInfo<Kind> page = new PageInfo<>(kinds,kind.getPageSize());
        return page;
    }

    @Override
    public Integer getCountListKind(Kind kind) {
        return kindMapper.getCountListKind(kind);
    }

    @Override
    public Integer deleteKindById(Kind kind) {
        int deleteKindResult;
        int result = bookMapper.getBookCountByKind_id(kind.getId());
        if(result==0){
            deleteKindResult = kindMapper.deleteKindById(kind);
        }else{
            deleteKindResult = -1;
        }
        return deleteKindResult;
    }

    @Override
    public Integer updateKindById(Kind kind) {
        int updateKindResult = kindMapper.updateKindById(kind);
        return updateKindResult;
    }

    @Override
    public Kind searchKindById(String id) {
        return kindMapper.searchKindById(id);
    }

    @Override
    public List<Kind> getAllListKind() {
        return kindMapper.getAllListKind();
    }
}
